package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import net.realsee.domain.wcs.work.bo.WcsWarnTypeBo;
import net.realsee.domain.wcs.work.vo.WcsWarnTypeVo;
import net.realsee.domain.wcs.work.WcsWarnType;
import net.realsee.mapper.wcs.work.WcsWarnTypeMapper;
import net.realsee.service.wcs.work.IWcsWarnTypeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 告警类型定义Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsWarnTypeServiceImpl extends ServiceImpl<WcsWarnTypeMapper, WcsWarnType> implements IWcsWarnTypeService {

    private final WcsWarnTypeMapper baseMapper;

    /**
     * 查询告警类型定义
     */
    @Override
    public WcsWarnTypeVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询告警类型定义列表
     */
    @Override
    public TableDataInfo<WcsWarnTypeVo> selectVoPage(WcsWarnTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarnType> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarnTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询告警类型定义列表
     */
    @Override
    public List<WcsWarnTypeVo> selectVoList(WcsWarnTypeBo bo) {
        LambdaQueryWrapper<WcsWarnType> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarnType> buildVoQueryWrapper(WcsWarnTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarnType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarnType()), WcsWarnType::getWarnType, bo.getWarnType());
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), WcsWarnType::getTypeName, bo.getTypeName());
        return lqw;
    }

    /**
     * 新增告警类型定义
     */
    @Override
    public Boolean insertByBo(WcsWarnTypeBo bo) {
        WcsWarnType add = BeanUtil.toBean(bo, WcsWarnType.class);
        return this.save(add);
    }

    /**
     * 修改告警类型定义
     */
    @Override
    public Boolean updateByBo(WcsWarnTypeBo bo) {
        WcsWarnType update = BeanUtil.toBean(bo, WcsWarnType.class);
        return this.updateById(update);
    }

    /**
     * 批量删除告警类型定义
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
